﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using WindowsSignInPage;
using Microsoft.SharePoint.Administration;
using System.IdentityModel.Tokens;
using Microsoft.IdentityModel.Claims;

namespace ADAuthTest
{
    class SharePointUser
    {
        const string SharePointUrl = "http://moss2010.ceibs.edu";
        public SharePointUser()
        {
            string admin = "moss2010";
            LoginUser loginUser = new LoginUser(admin);
            string password = EncryptTool.Decrypt(loginUser.Pwd);

            if (WindowsContext.Instance.ImpersonateValidUser(loginUser.LoginId, loginUser.Domain, password))
            {
                Authentication.SuccessLogin(loginUser.LoginId, password);
                using (SPSite site = new SPSite(SharePointUrl))
                {
                    SPWeb web = site.OpenWeb();
                    SPUser spuser = web.SiteUsers["factest2"];
                    //web.SiteUsers
                }
            }
            //WindowsSecurityToken token =
            //    WindowsContext.Instance.GetToken(user.LoginId, user.Domain, password);
            ////Authentication.SuccessLogin(user.LoginId, password);
            //IClaimsIdentity id = new ClaimsIdentity(token.WindowsIdentity);
            
            //SPUserToken spToken = new SPUserToken(id, new Uri(SharePointUrl));
            //using (SPSite site = new SPSite(SharePointUrl, spToken))
            //{
            //    SPWeb web = site.OpenWeb();
            //    SPUser spuser = web.SiteUsers["factest2"];
            //    //web.SiteUsers
            //}
        }
    }
}
